package com.virtualCat.controller.bankend;

import com.virtualCat.common.Const;
import com.virtualCat.common.Msg;
import com.virtualCat.entity.Cat;
import com.virtualCat.service.CatService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value = "/manage/cat/")
@ResponseBody
public class CatManageController {
    @Autowired
    private CatService catService;

    /**
     * 上传文件
     * @param file
     * @return
     */
    @RequestMapping(value = "upload",method = RequestMethod.POST)
    @ApiOperation(value="1.1.上传",httpMethod="POST",notes="图片上传")
    public String upload(HttpSession session,@RequestParam(value = "file") MultipartFile file,String flag){
        String value = (String) session.getAttribute(Const.admin_login);
        if (value!=null){
            String fileName = catService.uploadFile(file,flag);
            return fileName;
        }
        return "null";
    }

    /**
     * 多个上传
     * @param session
     * @param files
     * @return
     */
    @RequestMapping(value = "uploadFiles",method = RequestMethod.POST)
    @ApiOperation(value="1.2.图片上传",httpMethod="POST",notes="图片上传")
    public String[] upload(HttpSession session,@RequestParam(value = "files") MultipartFile[] files){
        String value = (String) session.getAttribute(Const.admin_login);
        if (value!=null){
            return catService.uploadFiles(files);
        }
        return null;
    }

    /**
     * 添加猫咪信息
     * @param session
     * @param cat
     * @return
     */
    @RequestMapping(value = "addCat",method = RequestMethod.POST)
    @ApiOperation(value="1.2.添加猫咪",httpMethod="POST",notes="添加猫咪")
    public Msg addCat(HttpSession session,@RequestBody Cat cat){
        String value = (String) session.getAttribute(Const.admin_login);
        if (value!=null){
            return catService.insertCat(cat);
        }
        return Msg.fail().add("msg","login");
    }

    /**
     * 查询某猫咪信息
     * @param session
     * @param catId
     * @return
     */
    @RequestMapping(value = "searchCat",method = RequestMethod.GET)
    @ApiOperation(value="1.2.查询猫咪",httpMethod="GET",notes="查询猫咪")
    public Msg findCat(HttpSession session,@RequestParam("catId") Integer catId){
        String value = (String) session.getAttribute(Const.admin_login);
        if (value!=null){
            return catService.findCatInfo(catId);
        }
        return Msg.fail().add("msg","login");
    }

    /**
     * 修改猫咪信息
     * @param session
     * @param cat
     * @return
     */
    @RequestMapping(value = "updateCat",method = RequestMethod.POST)
    @ApiOperation(value="1.2.修改猫咪",httpMethod="POST",notes="修改猫咪")
    public Msg updateCat(HttpSession session,@RequestBody Cat cat){
        String value = (String) session.getAttribute(Const.admin_login);
        if (value!=null){
            return catService.updateCat(cat);
        }
        return Msg.fail().add("msg","login");
    }

    /**
     * 分页查询猫咪信息
     * @param session
     * @param pageSize
     * @param pageNum
     * @return
     */
    @RequestMapping(value = "searchCatList",method = RequestMethod.GET)
    @ApiOperation(value="1.3.查询猫咪信息",httpMethod="GET",notes="查询猫咪信息")
    public Msg searchCatList(HttpSession session,
                             @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                             @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum){
        String value = (String) session.getAttribute(Const.admin_login);
        if (value!=null){
            return catService.searchCatList(pageNum,pageSize);
        }
        return Msg.fail().add("msg","login");
    }


    /**
     * 删除猫咪
     * @param session
     * @param catId
     * @return
     */
    @RequestMapping(value = "deleteCat",method = RequestMethod.DELETE)
    @ApiOperation(value="1.2.删除猫咪",httpMethod="DELETE",notes="删除猫咪")
    public Msg deleteCat(HttpSession session,@RequestParam("catId")Integer catId){
        String value = (String) session.getAttribute(Const.admin_login);
        if (value!=null){
            return catService.deleteCat(catId);
        }
        return Msg.fail().add("msg","login");
    }

}
